package com.mall.order.services;

import com.mall.order.OrderCancelService;
import com.mall.order.constants.OrderConstants;
import com.mall.order.dal.entitys.Order;
import com.mall.order.dal.entitys.OrderItem;
import com.mall.order.dal.entitys.Stock;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dal.persistence.StockMapper;
import com.mall.order.dto.CancelOrderRequest;
import com.mall.order.dto.CancelOrderResponse;
import com.mall.order.dto.OrderItemDto;
import com.mall.order.utils.ExceptionProcessorUtils;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Slf4j
@Component
@Service
public class OrderCancelServiceImpl implements OrderCancelService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;
    @Autowired
    StockMapper stockMapper;

    // 取消订单
    @Override
    public CancelOrderResponse cancelOrder(CancelOrderRequest cancelOrderRequest) {
        CancelOrderResponse cancelOrderResponse = new CancelOrderResponse();
        try {
            cancelOrderRequest.requestCheck();
            Example orderExample = new Example(Order.class);
            orderExample.createCriteria().andEqualTo("orderId", cancelOrderRequest.getOrderId());
            Order tmpOrder = new Order();
            tmpOrder.setStatus(OrderConstants.ORDER_STATUS_TRANSACTION_CANCEL);
            orderMapper.updateByExampleSelective(tmpOrder, orderExample);

            String orderId = orderMapper.selectByPrimaryKey(cancelOrderRequest.getOrderId()).getOrderId();

            //删除ordershopping
//            Example orderShippingExample = new Example(OrderShipping.class);
//            orderShippingExample.createCriteria().andEqualTo("orderId", orderId);
//            orderShippingMapper.deleteByExample(orderShippingExample);

            // 更改orderItem表中status(库存锁定-->库存释放)
            //1库存已锁定 2库存已释放 3-库存减扣成功
            orderItemMapper.updateStockStatus(2, orderId);
            List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
            // 修改商品对应库存信息
            for (OrderItem orderItem : orderItems) {
                Integer num = orderItem.getNum();
                Stock stock = new Stock();
                stock.setItemId(orderItem.getItemId());
                stock.setLockCount(-num);
                stock.setStockCount(num.longValue());
                stockMapper.updateStock(stock);
            }
            cancelOrderResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
            cancelOrderResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());

        } catch (Exception e) {
            log.error("OrderCancelServiceImpl.cancelOrder occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(cancelOrderResponse, e);
        }
        return cancelOrderResponse;
    }

    /**
     * 支付成功后消减库存锁定数量
     * @param goodsDto
     */
    @Override
    public void updatePayStock(OrderItemDto goodsDto) {
        Stock stock = new Stock();
        stock.setItemId(Long.parseLong(goodsDto.getItemId()));
        stock.setLockCount(-goodsDto.getNum());
        stock.setStockCount(0L);
        stockMapper.updateStock(stock);
    }

    @Override
    public void updateOrderItemStatus(String orderId) {
        orderItemMapper.updateStockStatus(3,orderId);
    }
}
